From 85f459ff65cd35b43b0d4161e57462539530c354 Mon Sep 17 00:00:00 2001 From: Debian Science Maintainers Date: Sat, 2 Mar 2019 14:59:35 +0000 Subject: [PATCH] Don't use gammaln on complex input In scipy >= 1.0, it rejects such input with TypeError: ufunc 'gammaln' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' Author: Kevin "bashtage" Sheppard, Rebecca N. Palmer Origin: upstream https://github.com/statsmodels/statsmodels/pull/3964/commits/c11c6242ff81a0f83d3e7fd83a8105397d3cbc1d Forwarded: not-needed Gbp-Pq: Name gammaln_complex.patch --- statsmodels/discrete/discrete_model.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/statsmodels/discrete/discrete_model.py b/statsmodels/discrete/discrete_model.py index db29bfb..6504a8b 100644 --- a/statsmodels/discrete/discrete_model.py +++ b/statsmodels/discrete/discrete_model.py @@ -22,6 +22,10 @@ __all__ = ["Poisson", "Logit", "Probit", "MNLogit", "NegativeBinomial"] from statsmodels.compat.python import lmap, lzip, range import numpy as np from scipy.special import gammaln +try: + from scipy.special import loggamma +except ImportError: + loggamma = gammaln from scipy import stats, special, optimize # opt just for nbin import statsmodels.tools.tools as tools from statsmodels.tools import data as data_tools @@ -2004,12 +2008,16 @@ class NegativeBinomial(CountModel): self._initialize() def _ll_nbin(self, params, alpha, Q=0): + if np.any(np.iscomplex(params)) or np.iscomplex(alpha): + gamma_ln = loggamma + else: + gamma_ln = gammaln endog = self.endog mu = self.predict(params) size = 1/alpha * mu**Q prob = size/(size+mu) - coeff = (gammaln(size+endog) - gammaln(endog+1) - - gammaln(size)) + coeff = (gamma_ln(size+endog) - gamma_ln(endog+1) - + gamma_ln(size)) llf = coeff + size*np.log(prob) + endog*np.log(1-prob) return llf -- 2.30.2